{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n",
      "??\n"
     ]
    }
   ],
   "source": [
    "# encoding: utf-8\n",
    "\n",
    "import pandas as pd\n",
    "from pandas import *\n",
    "import datetime\n",
    "import json\n",
    "from pymongo import MongoClient\n",
    "from collections import defaultdict\n",
    "\n",
    "pd.set_option('display.width', None)  # 设置字符显示宽度\n",
    "pd.set_option('display.max_rows', None)  # 设置显示最大行\n",
    "pd.set_option('display.max_columns', None)  # 设置显示最大行\n",
    "\n",
    "client = MongoClient('localhost', 27017)\n",
    "db = client.futures\n",
    "indexMarket = db.indexMarket\n",
    "unit=db.unit\n",
    "peak2=db.peak2\n",
    "\n",
    "start='20190601'\n",
    "# var='JD'\n",
    "\n",
    "indexMarket = DataFrame(list(indexMarket.find({'date': {'$gte': start}})))\n",
    "unit = DataFrame(list(unit.find()))\n",
    "dd=unit['variety']\n",
    "\n",
    "\n",
    "df2={}\n",
    "for i in dd:\n",
    "    try:\n",
    "        df=indexMarket[indexMarket['variety']==i]\n",
    "\n",
    "# df\n",
    "        df= df[['date', 'variety','set_close', 'set_high', 'set_low']].drop_duplicates()\n",
    "\n",
    "        df['date'] = pd.to_datetime(df['date'])\n",
    "        df.set_index('date',inplace=True)\n",
    "        maxs = df.rolling(window=10, on='variety').max().dropna()\n",
    "        mins = df.rolling(window=10, on='variety').min().dropna()\n",
    "        hb = pd.merge(maxs, mins, on=['date', 'variety'], how='outer')\n",
    "        hb['gains']=round(hb.apply(lambda x: (x['set_high_x'] / x['set_low_y']-1)*100, axis=1), 2)\n",
    "# hb\n",
    "# #         # 跌幅\n",
    "        hb['lesses'] = round(hb.apply(lambda x:(1-x['set_low_y'] / x['set_high_x'])*100,axis=1), 2)\n",
    "        hb.tail(2)\n",
    "        hb['peak']=hb.apply(lambda x:x['gains'] if x['set_low_y'] < x['set_close_x']else x['lesses'], axis=1)\n",
    "#         hb['variety']=i\n",
    "#         print(hb.tail(3))\n",
    "        hb = hb.reset_index()\n",
    "#         print(hb)\n",
    "#         hb=pd.DataFrame(hb)\n",
    "        peak2.insert_one(json.loads(hb.T.to_json()).values())\n",
    "        print(json.loads(hb.T.to_json()).values())\n",
    "\n",
    "    except:\n",
    "#         pass\n",
    "        print('??')\n",
    "#     continue\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "\n",
    "# maxs=df.groupby(['variety'])['set_high','set_low'].rolling(6).max()\n",
    "# maxs\n",
    "\n",
    "# maxs = df.rolling(window=10, on='variety').max().dropna()\n",
    "# mins = df.rolling(window=10, on='variety').min().dropna()\n",
    "# hb = pd.merge(maxs, mins, on=['date', 'variety'], how='outer')\n",
    "# hb['gains']=round(hb.apply(lambda x: (x['set_high_x'] / x['set_low_y']-1)*100, axis=1), 2)\n",
    "# # hb\n",
    "# # #         # 跌幅\n",
    "# hb['lesses'] = round(hb.apply(lambda x:(1-x['set_low_y'] / x['set_high_x'])*100,axis=1), 2)\n",
    "# hb.tail(2)\n",
    "# # hb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# hb['peak']=hb.apply(lambda x:x['gains'] if x['set_low_y'] < x['set_close_x']else x['lesses'], axis=1)\n",
    "\n",
    "# hb.tail(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 211,
   "metadata": {},
   "outputs": [],
   "source": [
    "#    for key, value in hb.items():\n",
    "#             print(key)\n",
    "# #                 df[key].addend(df2[key][])\n",
    "# #             df2[value].addend(value)\n",
    "# #             print(df2)\n",
    "# except:\n",
    "#     pass\n",
    "# continue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "metadata": {},
   "outputs": [],
   "source": [
    "#.dropna()  #,min_periods=1\n",
    "# # print(max.tail(50))\n",
    "# min = df.rolling(window=60, on='variety').min()#.dropna()\n",
    "# # print(min.tail(50))\n",
    "# hb = pd.merge(max, min, on=['date', 'variety'], how='outer')#.fillna(method='ffill').drop_duplicates()\n",
    "# hb1=pd.merge(df,hb,on=['date', 'variety'], how='outer')\n",
    "#         # print(data.head(5))\n",
    "\n",
    "# #         # 涨幅\n",
    "# # hb['gains'] = round((hb['set_high_x'] / hb['set_low_y']-1) * 100, 2)\n",
    "# hb['gains']=round(hb.apply(lambda x: (x['set_high_x'] / x['set_low_y']-1)*100, axis=1), 2)\n",
    "# # hb\n",
    "# # #         # 跌幅\n",
    "# hb['lesses'] = round(hb.apply(lambda x:(1-x['set_low_y'] / x['set_high_x'])*100,axis=1), 2)\n",
    "# hb\n",
    "# \n",
    "# hb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
